package com.service.admin.merchant.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.service.admin.admin.dto.LoginUserSession;
import com.service.admin.merchant.controller.rep.MerchantPageRep;
import com.service.admin.merchant.controller.req.MerchantAuditReq;
import com.service.admin.merchant.controller.req.MerchantPageReq;
import com.service.admin.merchant.service.MerchantAuditRecordService;
import com.service.admin.merchant.service.MerchantService;
import com.service.admin.support.SessionUtil;
import com.service.core.exception.CommonException;
import com.service.core.rest.response.CommonPageRep;
import com.service.dao.enums.MerchantEnums;
import com.service.dao.mapper.MerchantMapper;
import com.service.dao.model.Merchant;
import com.service.dao.model.MerchantAuditRecord;
import com.service.dao.po.MerchantPO;
import com.service.dao.query.MerchantQueryParam;
import com.service.utility.modelmapper.ModelMapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.stream.Collectors;


@Service("merchantService")
public class MerchantServiceImpl extends ServiceImpl<MerchantMapper, Merchant> implements MerchantService {

    @Autowired
    private MerchantAuditRecordService auditRecordService;

    @Autowired
    private SessionUtil sessionUtil;

    @Override
    public CommonPageRep<MerchantPageRep> page(MerchantPageReq pageReq) {
        Page<MerchantPO> page = PageHelper.startPage(pageReq.getPageNo(), pageReq.getPageSize());
        this.baseMapper.list4Admin(ModelMapperUtil.map(pageReq, MerchantQueryParam.class));
        return CommonPageRep.buildResultRep(page, page.getResult().stream().map(item -> ModelMapperUtil.map(item, MerchantPageRep.class)).collect(Collectors.toList()));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void audit(Integer merchantId, MerchantAuditReq auditReq) {

        Merchant merchant = getById(merchantId);
        if (ObjectUtils.isEmpty(merchant)) {
            throw new CommonException("不存在此商家信息");
        }

        if (!ObjectUtils.nullSafeEquals(merchant.getMerchantStatus(), MerchantEnums.Status.created.getCode())
                && !ObjectUtils.nullSafeEquals(merchant.getMerchantStatus(), MerchantEnums.Status.invalid.getCode())) {
            throw new CommonException("该商家已审核通过");
        }
        Integer merchantStatus;
        String auditMsg = auditReq.getAuditMsg();
        if (ObjectUtils.nullSafeEquals(auditReq.getAuditStatus(), MerchantEnums.AuditStatus.valid.getCode())) {
            merchantStatus = MerchantEnums.Status.in_business.getCode();
            auditMsg = MerchantEnums.AuditStatus.valid.getMsg();
        } else {
            merchantStatus = MerchantEnums.Status.invalid.getCode();
        }

        updateById(Merchant.builder().id(merchantId).merchantStatus(merchantStatus).build());
        LoginUserSession session = sessionUtil.getUser();
        auditRecordService.save(MerchantAuditRecord.builder()
                .merchantId(merchantId)
                .merchantName(merchant.getMerchantName())
                .auditStatus(auditReq.getAuditStatus())
                .auditMsg(auditMsg)
                .auditId(session.getId())
                .auditName(session.getLoginName())
                .build());

    }

    @Override
    public void batchUpdateMerchantStatus(Integer[] ids, MerchantEnums.Status status) {

        if (ObjectUtils.isEmpty(ids)) {
            throw new CommonException("请选择商家");
        }

        this.baseMapper.updateBatchMerchantStatus(ids, status.getCode());
    }


}
